"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dolphin插件
Case Name   : kill -9，再重启数据库后，测试插件正常使用
Create At   : 2023.07.25
Owner       : z@wan005
Description :
    1.创建兼容b库
    2.kill -9 opengauss
    3.连接b库查询插件是否存在
    4.重启数据库
    5.连接b库执行兼容语法
    6.连接默认a库执行兼容语法
    7.删除插件
    8.清理环境
Expect      :
    1.成功
    2.成功
    3.连接失败
    4.成功
    5.成功
    6.报错
    7.报错
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class Plugin027(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.common = Common()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.dbPrimaryRootNode = Node('PrimaryRoot')
        self.db_name = "db_plugin0027"

    def test_dolphin(self):
        text = '--step1:创建兼容b库;expect:成功--'
        self.log.info(text)
        sql_cmd = self.sh_primy.execut_db_sql(f"drop database if exists "
                                              f"{self.db_name};"
                                              f"create database "
                                              f"{self.db_name} "
                                              f"dbcompatibility ='b';")
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_cmd,
                      '执行失败' + text)

        text = '--step2:kill -9 opengauss;expect:成功--'
        self.log.info(text)
        result = self.common.kill_pid(self.dbPrimaryRootNode, '9')
        self.log.info(result)

        text = '--step3:连接b库查询插件是否存在;expect:连接失败---'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname " \
                  "='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd,
                                              dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('failed to connect', sql_res, '执行失败' + text)

        text = '--step4:重启数据库;expect:成功--'
        self.log.info(text)
        restart_msg = self.sh_primy.restart_db_cluster()
        self.log.info(restart_msg)
        status = self.sh_primy.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)

        text = '--step5:连接b库执行兼容语法;expect:成功--'
        self.log.info(text)
        sql_cmd = "select regexp('a', true);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd,
                                              dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual('1', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step6:连接默认a库执行兼容语法;expect:报错--'
        self.log.info(text)
        sql_cmd = "select regexp('a', true);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(
            'ERROR:  function regexp(unknown, boolean) does not exist',
            sql_res, '执行失败' + text)

        text = '--step7:删除插件;expect:报错--'
        self.log.info(text)
        sql_cmd = "drop extension dolphin;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd,
                                              dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('ERROR:  EXTENSION is not yet supported', sql_res,
                      '执行失败' + text)

    def tearDown(self):
        text = '---step8:清理环境;expect:清理环境完成----'
        self.log.info(text)
        cmd = f"drop database if exists {self.db_name};"
        res = self.sh_primy.execut_db_sql(cmd)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
